package plugins.lxfa.animatePlayer
{
    
    import core.manager.sceneManager.*;
    import core.manager.scriptManager.*;
    
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;
    
    import memory.*;
    
    import mx.core.*;
    import mx.managers.*;
    
    import plugins.lxfa.animatePlayer.model.*;
    
    import view.loadings.*;
    import view.player.*;

    public class AnimatePlayer extends EventDispatcher implements IAnimatePlayer
    {
        private var animate:SwfPlayer;
        private var delayKey:int;
        private var loading:LoadingWaveRota;
        private static var _instance:AnimatePlayer;
        private var currentMusic:String="";
        private var currentId:int=-1;                                   //当前精灵的ID
        private var isAlive:Boolean=false;                              //精灵处于出场，说话，退场过程中为true，其他为false
        private var id:int=-1;
        public function AnimatePlayer()
        {
            if (_instance == null)
            {
                _instance = this;
                AnimatePlayerProxy.getInstance().animatePlayer=this;
            }
            else
            {
                throw new Error("AnimatePlayer不能被实例化");
            }
            return;
        }// end function

        private function delayShow() : void
        {
            clearInterval(delayKey);
            AnimateMusicManager.getInstance().removeEventListener(Event.COMPLETE, downLoaded);
            AnimateMusicManager.getInstance().dispose();
            this.showIn();
            return;
        }// end function

        public function showAnimate(id:int) : void
        {
            if (id != -1)
            {
            	this.id=id;
                removeAnimate();
                isAlive=true;
                PopUpManager.addPopUp(animate, DisplayObject(Application.application), false);
                PopUpManager.centerPopUp(animate);
                animate.y = 270;
                animate.x = 500;
                AnimatePlayerModel.getInstance().init(id);
                dispatchEvent(new AnimatePlayerEvent(AnimatePlayerEvent.REFRESH_TEXT_STATE, "", AnimatePlayerModel.getInstance().hasText));
                currentMusic=AnimatePlayerModel.getInstance().musicUrl;
                AnimateMusicManager.getInstance().loadBackGroundMusic(currentMusic);
                AnimateMusicManager.getInstance().addEventListener(Event.COMPLETE, downLoaded);
                SceneManager.getInstance().addEventListener(SceneChangeEvent.INIT, on_init);
                if(AnimatePlayerModel.getInstance().hasText)
                {
                	currentId=id;
                }
            }
            return;
        }// end function

        private function on_enter_frame(e:Event) : void
        {
            var _loc_2:Object;
            if (AnimatePlayerModel.getInstance().textsLength > 0)
            {
                if (AnimateMusicManager.getInstance().getPosition() > AnimatePlayerModel.getInstance().getNearestTime())
                {
                    _loc_2 = AnimatePlayerModel.getInstance().getNearestTask();
                    ScriptManager.getInstance().runScriptDirectly(_loc_2.script);
                }
            }
            else
            {
                Application.application.removeEventListener(Event.ENTER_FRAME, on_enter_frame);
            }
            return;
        }// end function

        private function downLoaded(e:Event) : void
        {
            delayKey = setInterval(delayShow, 500);
            return;
        }// end function

        private function refreshText(e:String) : void
        {
            this.dispatchEvent(new AnimatePlayerEvent(AnimatePlayerEvent.REFRESH_TEXT, e));
            return;
        }// end function

        private function showIn() : void
        {
            this.dispatchEvent(new AnimatePlayerEvent(AnimatePlayerEvent.SHOW_ANIMATE_IN,"",true,AnimatePlayerModel.getInstance().hasCloseButton,id));
            in_music_complete(null);
            return;
        }// end function
        /**
        * 进场声音播放完毕时调用
        */ 
        private function in_music_complete(e:Event):void
        {
        	AnimateMusicManager.getInstance().dispose();
        	AnimateMusicManager.getInstance().loadBackGroundMusic(currentMusic);
        	AnimateMusicManager.getInstance().play();
        	Application.application.addEventListener(Event.ENTER_FRAME, on_enter_frame);
        }
        private function showSay() : void
        {
            this.dispatchEvent(new AnimatePlayerEvent(AnimatePlayerEvent.SHOW_ANIMATE_SAY));
            return;
        }// end function

        public function init() : void
        {
            ScriptManager.getInstance().addApi(ScriptName.REFRESH_ANIMATE_TEXT, refreshAnimateText);
            ScriptManager.getInstance().addApi(ScriptName.REMOVE_ANIMATE, removeAnimate);
            ScriptManager.getInstance().addApi(ScriptName.ADD_ANIMATE, showAnimate);
            ScriptManager.getInstance().addApi(ScriptName.SHOW_ANIMATE_OUT, showOut);
            if (animate == null)
            {
                animate = new SwfPlayer("swf/animate/animate.swf", 900, 480);
                animate.addEventListener(Event.COMPLETE, function (e:Event) : void
                {
	                dispatchEvent(new AnimatePlayerEvent(AnimatePlayerEvent.ALL_STOP));
	                dispatchEvent(e);
	                if (loading != null)
	                {
	                    loading.dispose();
	                    loading = null;
	                }
                }// end function
            );
            }
            return;
        }// end function

        private function showOut() : void
        {
            this.dispatchEvent(new AnimatePlayerEvent(AnimatePlayerEvent.SHOW_ANIMATE_OUT));
            AnimateMusicManager.getInstance().dispose();
            AnimateMusicManager.getInstance().loadBackGroundMusic("http://audio.pavilion.expo.cn/p5006/audio/animate/out.mp3",false);
            AnimateMusicManager.getInstance().play();
            AnimateMusicManager.getInstance().addEventListener(Event.SOUND_COMPLETE,out_music_complete);
            return;
        }// end function
        private function out_music_complete(e:Event):void
        {
        	removeAnimate();
        }
        private function on_init(_embed_css_Assets_swf_TreeFolderOpen_659911006:SceneChangeEvent) : void
        {
            SceneManager.getInstance().removeEventListener(SceneChangeEvent.INIT, on_init);
            removeAnimate();
            return;
        }// end function

        public function refreshAnimateText(_embed_css_Assets_swf_TreeFolderOpen_659911006:String) : void
        {
            this.dispatchEvent(new AnimatePlayerEvent(AnimatePlayerEvent.REFRESH_TEXT, _embed_css_Assets_swf_TreeFolderOpen_659911006));
            return;
        }// end function

        public function removeAnimate() : void
        {
            this.dispatchEvent(new AnimatePlayerEvent(AnimatePlayerEvent.ALL_STOP));
            clearInterval(delayKey);
            MemoryRecovery.getInstance().gcFun(AnimateMusicManager.getInstance(), Event.COMPLETE, downLoaded);
            MemoryRecovery.getInstance().gcFun(Application.application, Event.ENTER_FRAME, on_enter_frame);
            MemoryRecovery.getInstance().gcFun(SceneManager.getInstance(), SceneChangeEvent.INIT, on_init);
            MemoryRecovery.getInstance().gcFun(AnimateMusicManager.getInstance(),Event.SOUND_COMPLETE,in_music_complete);
            AnimateMusicManager.getInstance().dispose();
            AnimatePlayerModel.getInstance().dispose();
            if (animate.parent != null)
            {
                PopUpManager.removePopUp(animate);
            }
            isAlive=false;//标志精灵死亡
            return;
        }// end function
        /**
        * 重新显示精灵
        */ 
        public function reshowAnimate():void
        {
        	if(!isAlive)
        	{
        		showAnimate(currentId);
        	}
        }
        public static function getInstance() : AnimatePlayer
        {
            if (_instance == null)
            {
                return new AnimatePlayer;
            }
            return _instance;
        }// end function
    }
}
